package android.slc.mp.ui.page.audio;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.Audio.AudioColumns.DURATION;
import static android.provider.MediaStore.MediaColumns.DATA;
import static android.provider.MediaStore.MediaColumns.DATE_MODIFIED;
import static android.provider.MediaStore.MediaColumns.DISPLAY_NAME;
import static android.provider.MediaStore.MediaColumns.SIZE;

import android.database.Cursor;
import android.provider.MediaStore;
import android.slc.ml.MediaLoader;
import android.slc.ml.callback.audio.OnAudioLoaderOnBaseCallBack;
import android.slc.ml.utils.MediaLoaderFileUtils;
import android.slc.ml.utils.MediaLoaderUriUtils;
import android.slc.mp.R;
import android.slc.mp.SlcMp;
import android.slc.mp.po.AudioFolder;
import android.slc.mp.po.AudioItem;
import android.slc.mp.po.AudioResult;
import android.slc.mp.po.SelectEvent;
import android.slc.mp.po.i.IAudioFolder;
import android.slc.mp.po.i.IAudioItem;
import android.slc.mp.po.i.IAudioResult;
import android.slc.mp.ui.SlcIMpDelegate;
import android.slc.mp.ui.page.SlcMpPagerBaseVirtualVm;
import android.slc.mp.utils.SlcMpMediaBrowseUtils;

import androidx.fragment.app.FragmentActivity;
import androidx.loader.content.Loader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by slc
 */
public class SlcMpPagerAudioVirtualVm extends SlcMpPagerBaseVirtualVm<IAudioResult, IAudioFolder, IAudioItem> {

    public SlcMpPagerAudioVirtualVm(SlcIMpDelegate mediaPickerDelegate) {
        super(SlcMp.MEDIA_TYPE_AUDIO, mediaPickerDelegate);
    }

    @Override
    public void loader(FragmentActivity fragmentActivity) {
        MediaLoader.getLoader().loadAudios(fragmentActivity, new OnAudioLoaderOnBaseCallBack<IAudioResult>(getMediaPickerDelegate().getFilePropertyWithMediaType(getMediaType())) {

            @Override
            protected IAudioResult generateResultDoInBackground(Loader<Cursor> loader, Cursor data) {
                List<IAudioFolder> folders = new ArrayList<>();

                IAudioFolder folder, allItemFolder = new AudioFolder();

                if (data != null) {
                    IAudioItem item;
                    while (data.moveToNext()) {
                        long audioId = data.getLong(data.getColumnIndexOrThrow(_ID));
                        String name = data.getString(data.getColumnIndexOrThrow(DISPLAY_NAME));
                        String path = data.getString(data.getColumnIndexOrThrow(DATA));
                        long duration = data.getLong(data.getColumnIndexOrThrow(DURATION));
                        long size = data.getLong(data.getColumnIndexOrThrow(SIZE));
                        long modified = data.getLong(data.getColumnIndexOrThrow(DATE_MODIFIED));
                        item = new AudioItem();
                        item.setExtension(MediaLoaderFileUtils.getFileExtension(path));
                        item.setMediaTypeTag(getMediaType());
                        item.setId(audioId);
                        item.setDisplayName(name);
                        item.setPath(path);
                        item.setUri(MediaLoaderUriUtils.id2Uri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audioId));
                        item.setDuration(duration);
                        item.setSize(size);
                        item.setModified(modified);
                        String parent = path.substring(0, path.lastIndexOf(File.separator));
                        folder = new AudioFolder(parent, parent.substring(parent.lastIndexOf(File.separator) + 1));
                        if (folders.contains(folder)) {
                            folders.get(folders.indexOf(folder)).addItem(item);
                        } else {
                            folder.addItem(item);
                            folders.add(folder);
                        }
                        allItemFolder.addItem(item);
                    }
                }
                //此处不用添加headItem
                allItemFolder.setName(SlcMp.getInstance().getApp().getString(R.string.slc_m_p_all_music));
                folders.add(0, allItemFolder);
                return new AudioResult(folders);
            }

            @Override
            public void onResult(IAudioResult result) {
                SlcMpPagerAudioVirtualVm.this.onResult(result);
            }

        });
    }

    @Override
    public void onItemClick(int position) {
        SlcMpMediaBrowseUtils.playerAudio(getMediaPickerDelegate().getContext(), getCurrentMediaList().get(position).getUri());
    }

    @Override
    public void onItemChildClick(int position) {
        SlcMpMediaBrowseUtils.playerAudio(getMediaPickerDelegate().getContext(), getCurrentMediaList().get(position).getUri());
    }

}
